



import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class JTLECSV {
	String creationDate = null;
	String digitsOfPI = null;
	String numOfUsers = null;
	String numOfIteration = null;
	String targetHost = null;
	String srcFileName = null;
	String outFileName = null;
	String extension = "csv";
	BufferedWriter out = null;
	//No generics
	//List myEmpls;
	Document dom;


	public JTLECSV(String[] args){
		//create a list to hold the employee objects
		//myEmpls = new ArrayList();
		this.creationDate = args[0];
		this.digitsOfPI = args[1];
		this.numOfUsers = args[2];
		this.numOfIteration = args[3];
		this.targetHost = args[4];
		this.outFileName = args[5];
		this.srcFileName = args[6];
	}

	public void convert() {

		//parse the xml file and get the dom object
		parseXmlFile();

		//get each employee element and create a Employee object
		parseDocument();

		//Iterate through the list and print the data
		//printData();

	}


	private void parseXmlFile(){
		//get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		try {

			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();

			//parse using builder to get DOM representation of the XML file
			dom = db.parse(srcFileName);


		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}


	private void parseDocument(){
		//get the root elememt
		Element docEle = dom.getDocumentElement();

		//get a nodelist of <sample> elements
		NodeList nl = docEle.getElementsByTagName("sample");
		if(nl != null && nl.getLength() > 0) {
			try {
				
					out = new BufferedWriter(new FileWriter(outFileName, true));

				for(int i = 0 ; i < nl.getLength();i++) {

					//get the employee element
					Element el = (Element)nl.item(i);

					//get the Employee object
					writeOutput(el);

				}
				out.close();
			}catch(IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}


	/**
	 * I take an employee element and read the values in, create
	 * an Employee object and return it
	 * @param sample
	 * @return
	 */
	private void writeOutput(Element sample) throws IOException{

		//for each <employee> element get text or int values of 
		//name ,id, age and name
		//		String t = getTextValue(empEl,"Name");
		//		int id = getIntValue(empEl,"Id");
		//		int age = getIntValue(empEl,"Age");

		String t = sample.getAttribute("t");
		String ts = sample.getAttribute("ts");
		String na = sample.getAttribute("na");
		//System.out.println(time + "," + timeStamp);
		
		out.write(creationDate+","+digitsOfPI +"," +numOfUsers +"," +numOfIteration+","+targetHost+","+ts +","+t + ","+na + "\n");



	}


	/**
	 * I take a xml element and the tag name, look for the tag and get
	 * the text content 
	 * i.e for <employee><name>John</name></employee> xml snippet if
	 * the Element points to employee node and tagName is name I will return John  
	 * @param ele
	 * @param tagName
	 * @return
	 */
	private String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}

		return textVal;
	}


	/**
	 * Calls getTextValue and returns a int value
	 * @param ele
	 * @param tagName
	 * @return
	 */
	private int getIntValue(Element ele, String tagName) {
		//in production application you would catch the exception
		return Integer.parseInt(getTextValue(ele,tagName));
	}


	public static void main(String[] args){
		if(args.length < 7) {
			System.out.println("Usage: program <date> <pi> <users> <iteration> <system> <out file name> <src file name> ");
			System.exit(-1);
		}
		//create an instance
		//System.out.println("Converting: " + args[0]);
		JTLECSV jc = new JTLECSV(args);

		//call run
		jc.convert();
	}
}
